package com.itheima.Service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Service.SetemalDishService;
import com.itheima.Service.SetmealService;
import com.itheima.bean.Category;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.common.PageParam;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.SetmealDao;
import com.itheima.dao.SetmealDishDao;
import com.itheima.dto.SetmealDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private SetemalDishService setemalDishService;

    /*    @Autowired
        private com.itheima.service.CategoryService categoryService;*/
    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private SetmealDishDao setmealDishDao;

    /**
     * 根据分类id查询套餐数据
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> findByCategoryId(long categoryId) {

        //构建条件对象
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        //设置条件
        lqw.eq(Setmeal::getCategoryId, categoryId);

        return setmealDao.selectList(lqw);
    }

    /**
     * 新增套餐
     * 1.检验套餐名称是否重复 重复返回-1
     * 2.新增套餐表，再新增套餐菜品表 正常新增完成返回 1
     * 3.新增产生异常，返回0
     * @param setmealDto
     * @return
     */
    @Override
    public int add(SetmealDto setmealDto) {
        try {
            LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
            slqw.eq(Setmeal::getName,setmealDto.getName());
            Setmeal setmeal1 = setmealDao.selectOne(slqw);
            if (setmeal1!=null){
                return -1;
            }

            Setmeal setmeal = new Setmeal();
            BeanUtils.copyProperties(setmealDto,setmeal);
            setmeal.setCode("123");
            setmealDao.insert(setmeal);

            setmealDto.getSetmealDishes().stream().forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setSort(14);
                setmealDishDao.insert(setmealDish);
            });
        } catch (BeansException e) {
            e.printStackTrace();
            return 1;
        }
        return 0;
/*         int row1 = setmealDao.insert(setmealDto);

       List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();

        int row2 = 0;


        if (setmealDishList.size() > 0) {
            for (SetmealDish setmealDish : setmealDishList) {

                setmealDish.setSetmealId(setmealDto.getId());


                row2 += setemalDishService.add(setmealDish);
            }

        }


        return row1 > 0 && row2 == setmealDishList.size() ? 1 : 0;*/
    }

    @Override
    public IPage<SetmealDto> findPage(PageParam pageParam) {


        IPage<Setmeal> setmealIPage = new Page<>(pageParam.getPage(), pageParam.getPageSize());

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();


        lqw.like(pageParam.getName() != null, Setmeal::getName, pageParam.getName());

        setmealDao.selectPage(setmealIPage, lqw);

        IPage<SetmealDto> dtoIPage = new Page<>();

        dtoIPage.setTotal(setmealIPage.getTotal());

        List<SetmealDto> dtoList = setmealIPage.getRecords().stream().map(setmeal -> {


            SetmealDto dto = new SetmealDto();

            BeanUtils.copyProperties(setmeal, dto);


            Category category = categoryDao.selectById(setmeal.getCategoryId());//category对象为空-》setmeal内的catogeryid不存在

            dto.setCategoryName(category.getName());

            return dto;


        }).collect(Collectors.toList());


        dtoIPage.setRecords(dtoList);


        return dtoIPage;
    }

    /**
     * 删除套餐
     * 使用循环遍历套餐id
     * 在循环过程中把套餐的菜品删除
     * @param ids
     * @return
     */
    @Override
    public int delete(List<Long> ids) {

     /*   ids.stream().forEach(id->{
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

            lqw.eq(Setmeal::getStatus, 1);

            lqw.in(Setmeal::getId, ids);

            int total  =setmealDao.selectCount(lqw);
            if (total > 0) {

                throw new ClassCastException("该套餐有关联的 菜品禁止删除");
            }
        });*/
        try {
            ids.stream().forEach(id->{
                setmealDao.deleteById(id);
                LambdaQueryWrapper<SetmealDish> smd = new LambdaQueryWrapper<>();
                smd.eq(SetmealDish::getSetmealId,id);
                setmealDishDao.delete(smd);
            });
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return  0;
        }
    }

    @Override
    public boolean updateStatus(int status, Long[] ids) {
        int a=0;
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        for (Long id : ids) {
            setmeal.setId(id);
            setmealDao.updateById(setmeal);
            a++;
        }

        if (a==ids.length){
            return true;
        }
        return false;
    }

    @Override
    public List<Setmeal> findList(Long categoryId, int status) {
        LambdaQueryWrapper<Setmeal> sm = new LambdaQueryWrapper<>();
        sm.eq(Setmeal::getCategoryId,categoryId);
        sm.eq(Setmeal::getStatus,status);
        List<Setmeal> setmeals = setmealDao.selectList(sm);
        return setmeals;
    }
}
